package chapter1.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolTest {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newSingleThreadExecutor();//创建一个单一的线程池
        //ExecutorService threadPool = Executors.newFixedThreadPool(5);//创建一个固定大小的线程池，参数填线程池大小
        //ExecutorService threadPool = Executors.newCachedThreadPool();//创建一个可伸缩的线程池，遇强则强，遇弱则弱
        try {
            for (int i = 1; i <= 10; i++) {
                //在execute中丢入一个Runnable --->  lambda 表达式
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName());
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //使用完毕后，程序结束，关闭线程池
            threadPool.shutdown();
        }
    }
}
